﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MedicalCare.DbLayer.Adaptors;
using MedicalCare.Messages;
using MedicalCare.Translator;
using StructureMap;
using MedicalCare.EntityDefinitions.Entities;
using MedicalCare.Utilities;

namespace MedicalCare.Admin
{
    public partial class Schedule : System.Web.UI.Page
    {
        private const string OrderKey = "fam.Name";
        private const string OrderValue = "DESC";
        private string _whereString = "1=1";
        private IList<UserEntity> nurseList;
        private IList<ddlItem> timeList;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                HiddenSchedule();
                CalendarSchedule.SelectedDate = DateTime.Today;

                InitialSchedule();
            }
        }

        private void ShowSchedule()
        {
            ScheduleList.Visible = true;
            btnShowNewSchedule.Visible = false;
        }
        private void HiddenSchedule()
        {
            ScheduleList.Visible = false;
            btnShowNewSchedule.Visible = true;
        }

        private void InitialSchedule()
        {
            InitialTime();
            InitialNurseList();

            var message = new ScheduleMessage()
            {
                Date = CalendarSchedule.SelectedDate.Date
            };
            var scheduleList = CheckSchedule(message);
            if (scheduleList.Count > 0)
            {
                // 显示已存在的schedule
                var objPds = new PagedDataSource { DataSource = scheduleList };
                ScheduleList.DataSource = objPds;
                ScheduleList.DataBind();
            }
        }

        private void CreateNewSchedule()
        {
            InitialTime();
            InitialNurseList();
            InitialNewScduleList();
        }

        private void InitialNurseList()
        {
            var userAdaptor =  ObjectFactory.GetInstance<IUserAdaptor>();
            nurseList = userAdaptor.GetUsers("Name", "DESC", " RoleCode = 'NURSE' ");
        }

        private void InitialTime()
        {
            timeList = new List<ddlItem>();
            var dt = DateTime.Today;
            var endTime = 18;
            for (int startTime = 8; startTime <= endTime; startTime++)
            {
                timeList.Add(new ddlItem(startTime + ":00", startTime.ToString()));
            }
        }

        private IList<ScheduleEntity> CheckSchedule(ScheduleMessage message)
        {
            var scheduleAdaptor = ObjectFactory.GetInstance<IScheduleAdaptor>();

            _whereString = " StartDT >= '" + message.Date + "' AND StartDT < '" + message.Date.AddHours(24) +"' ";
            var scheduleEntityList = scheduleAdaptor.GetSchedule(OrderKey, OrderValue, _whereString);
            if (scheduleEntityList.Count > 0)
                ShowSchedule();
            else
                HiddenSchedule();
            return scheduleEntityList;
        }

        private void InitialNewScduleList()
        {
            var translator = ObjectFactory.GetInstance<IFamilyListToScheduleList>();
            var familyAdaptor = ObjectFactory.GetInstance<IFamilyAdaptor>();
            _whereString = "1=1";
            var source = translator.Translate(familyAdaptor.GetFamily(OrderKey, OrderValue, _whereString));

            var objPds = new PagedDataSource { DataSource = source};
            ScheduleList.DataSource = objPds;
            ScheduleList.DataBind();
        }

        protected void ScheduleList_ItemCommand(object source, DataListCommandEventArgs e)
        {

        }

        protected void ScheduleList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            var lbNurseId = (Label)(e).Item.FindControl("lbNurseId");
            var lbStartDT = (Label)(e).Item.FindControl("lbStartDT");
            var lbEndDT = (Label)(e).Item.FindControl("lbEndDT");


            var ddlNurse = (DropDownList)e.Item.FindControl("ddlNurse");
            ddlNurse.DataSource = nurseList;
            ddlNurse.DataBind();
            ddlNurse.SelectedValue = lbNurseId.Text;

            var ddlStartTime = (DropDownList)e.Item.FindControl("ddlStartTime");
            ddlStartTime.DataSource = timeList;
            ddlStartTime.DataBind();
            ddlStartTime.SelectedValue = DateTime.Parse(lbStartDT.Text).Hour.ToString();

            var ddlEndTime = (DropDownList)e.Item.FindControl("ddlEndTime");
            ddlEndTime.DataSource = timeList;
            ddlEndTime.DataBind();
            ddlEndTime.SelectedValue = DateTime.Parse(lbEndDT.Text).Hour.ToString();
        }

        protected void ScheduleList_DataBinding(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var scheduleAdaptor = ObjectFactory.GetInstance<IScheduleAdaptor>();

            foreach (var item in ScheduleList.Items)
            {
                var ddlNurse = (DropDownList)((DataListItem)item).FindControl("ddlNurse");
                var lbScheduleId = (Label)((DataListItem)item).FindControl("lbScheduleId");
                var lbFamilyId = (Label)((DataListItem)item).FindControl("lbFamilyId");
                var lbStatus = (Label)((DataListItem)item).FindControl("lbStatus");
                var ddlStartTime = (DropDownList)((DataListItem)item).FindControl("ddlStartTime");
                var ddlEndTime = (DropDownList)((DataListItem)item).FindControl("ddlEndTime");

                var msg = new ScheduleMessage()
                              {
                                  Id = int.Parse(lbScheduleId.Text),
                                  NurseId = int.Parse(ddlNurse.SelectedValue),
                                  FamilyId = int.Parse(lbFamilyId.Text),
                                  StartDT = CalendarSchedule.SelectedDate.Date.AddHours(double.Parse(ddlStartTime.SelectedValue)),
                                  EndDT = CalendarSchedule.SelectedDate.Date.AddHours(double.Parse(ddlEndTime.SelectedValue)),
                                  Status = lbStatus.Text
                              };

                scheduleAdaptor.Update(msg);
            }
        }

        protected void ScheduleList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void CalendarSchedule_SelectionChanged(object sender, EventArgs e)
        {
            InitialSchedule();
        }

        protected void btnShowNewSchedule_Click(object sender, EventArgs e)
        {
            ShowSchedule();
            CreateNewSchedule();
        }

    }
}